package se.liu.johfa428.game.objects.levelselection;

import java.awt.Color;

import se.liu.johfa428.levelstate.*;
import se.liu.johfa428.levelstate.LevelState.State;
import se.liu.oschi129.animation.animationspace.AnimationSpace;
import se.liu.oschi129.font.fontspace.FontSpace;
import se.liu.oschi129.level.levelspace.LevelInterface;
import se.liu.oschi129.world.interfaces.InputAction;
import se.liu.oschi129.world.model.*;
import se.liu.oschi129.world.objects.WorldObject;
import se.liu.oschi129.world.objects.WorldStaticObject;

/**
 * The platforms that visually represents the levels in the level selection menu. 
 * @author johfa428
 *
 */
public class ObjectLevelSelectionPlatform extends WorldStaticObject implements InputAction{

	private int levelNumber;
	private boolean loadLevel = false;
	private LevelState levelState;
	private LevelInterface level;
	
	@SuppressWarnings("unused")
	public ObjectLevelSelectionPlatform(double x, double y, LevelInterface level, LevelState levelState, int levelNumber) {
		super(Type.SOFT, AnimationSpace.get("img_platform"), true, x, y, 57/2,46/2);
		setImageSpeed(0);
		setImageIndex(levelState.getIndex());	
		this.levelNumber = levelNumber;
		this.level = level;
		
		if (levelState == null)
			this.levelState = new LevelState(State.NORMAL);
		else
			this.levelState = levelState;
	}

	public void performInputAction(WorldInput input) {
		if (input.jumpDown())
			loadLevel = true;
	}

	@Override public void performDrawingAction(WorldGraphics worldg){

		super.performDrawingAction(worldg);
		
		if (levelState.getIndex() == 2){
			worldg.setColor(Color.WHITE);
			worldg.setFont(FontSpace.get("font_status",12f));
			worldg.drawText(getX()+13,getY()+18, String.format("%01d",levelNumber));
		}	
	}

	@Override public void performStepAction(World world) {
		super.performStepAction(world);
		setImageIndex(levelState.getIndex());
		
		if (loadLevel){		
			loadLevel = false;
			for ( WorldObject other : world.getObjects()){
				if(other instanceof ObjectLevelSelectionPlayer && collision(other)){
					if (level != null) {
						world.pushSyncrhonized();
						level.createWorld(world, levelState);
					}
				}
			}			
		}
	}

}
